package portal.cmd;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.ShellExec;
import utils.Log4jInit;
@Component
@Scope("singleton")
public class JHCmd {
//	private String CMD_HOME="/apps/jhinno/unischeduler/bin";  
	private String CMD_HOME="/apps/NSE/bin";
	@Value("${COM_HOME}")
	public void setCMD_HOME(String cmd_home){
		CMD_HOME=cmd_home;
	}
	public  boolean linu_86_64(String exe,String data){
		return linu_86_64(exe,data,null);		
	}
	public  boolean linu_86_64(String exe,String data,String ret){
		Log4jInit.logger.info("**********JHCMD: "+CMD_HOME+"/linux-x86_64/"+exe+" "+data);
		return ShellExec.callShell(CMD_HOME+"/linux-x86_64/"+exe+" "+data,ret);		
	}
	public boolean jSchedule(String oper){
		return ShellExec.callShell(CMD_HOME+"/jhscheduler "+oper);
	}
	public boolean addHost(String hostName){
		return linu_86_64("jaddhost",hostName);
	}
	
	//hzj-190701
	public boolean killJob(String jobId){
		return linu_86_64("jctrl kill ",jobId,null);
	}
	
	
	public boolean removeHost(String hostName){
		return linu_86_64("jrmhost",hostName,"removed.");
	}
	public boolean limRegconfig(){
		return linu_86_64("jadmin limreconfig","",null);
	}
	public boolean hostOper(String hostName,String oper){
		switch(oper){
		case "open":return hostOpen(hostName);
		case "close":return hostClose(hostName);
		default:;break;
		}
		return false;
	}
	public boolean hostOpen(String hostName){
		//return linu_86_64("jadmin hopen",hostName);
		return hostOpen(hostName,"done");
	}
	public boolean hostOpen(String hostName,String ret){
		return linu_86_64("jadmin hopen ",hostName,ret);
	}
	public boolean hostClose(String hostName){
		//return linu_86_64("jadmin hclose",hostName);
		return hostClose(hostName, "done");
	}
	public boolean hostClose(String hostName,String ret){
		return linu_86_64("jadmin hclose ",hostName,ret);
	}
	
	public boolean queueOper(String queueName,String oper){
		switch(oper){
		case "open":return queueOpen(queueName);
		case "close":return queueClose(queueName);
		default:;break;
		}
		return false;
	}
	public boolean queueOpen(String queueName){
		return linu_86_64("jadmin qopen",queueName,"opened");
	}
	public boolean queueClose(String queueName){
		return linu_86_64("jadmin qclose",queueName,"closed");
	}
	public boolean jSchedStart(){
		return jSchedule("start");
	}
	public boolean jSchedStop(){
		return jSchedule("stop");
	}
	public boolean jSchedStatus(){
		return jSchedule("status");
	}
	public boolean jSchedRestart(){
		return jSchedule("restart");
	}
	public boolean jSchedReconfig(){
		return ShellExec.callShell("jadmin schedreconfig","Reconfiguration initiated");
	}
	public String  jhosts(){
		return jhosts("");//return ShellExec.command("jhosts");
	}
	public String jhosts_l(){
		return jhosts("-l");
	}
	public String jhosts_l_host(String hostName){
		return jhosts("-l",hostName);
	}
	public String jhosts_list_stat(){
		return jhosts("-l","stat");
	}
	public String jhosts_attrib(){
		return jhosts("attrib");
	}
	public String jhosts(String... param){
		String cmd="";
		if(param!=null)
			for(String s:param)
				cmd +=" "+s;
		return ShellExec.command("jhosts "+cmd );
	}
	public String jhostgroup(){
		return ShellExec.command("jhostgroup");
	}
}
